
$(function() {
    validateRule();
    $(".i-checks").iCheck({checkboxClass:"icheckbox_square-green-login"});
	$('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
    $('.radio-box').iCheck('check');
    // $('.radio-box').on('click',function (item) {
    //     alert('111')
    // });
});

$.validator.setDefaults({
    submitHandler: function() {
		login();
    }
});

function login() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var inviteId = '1';
    // var inviteId = $.common.trim($("input[name='inviteId']").val());
    // var validateCode = $("input[name='validateCode']").val();
    // var rememberMe = $("input[name='rememberme']").is(':checked');
    var pey_set_user_reg_type = $('input[name="pey_set_user_reg_type"]:checked').val();
    $.ajax({
        type: "post",
        url: ctx + "regist",
        data: {
            "username": username,
            "password": password,
            "inviteId" : inviteId,
            "pey_set_user_reg_type": pey_set_user_reg_type,
        },
        success: function(r) {
            if (r.code == 0) {
                location.href = '/regist/auth';
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true,
                minlength: 5,
                maxlength: 20
            },
            password: {
                required: true,
                minlength: 5,
                maxlength: 20
            },
            confirm: {
                required: true,
                equalTo: "#password"
            },
            rememberme: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
                minlength: icon + "用户账号不能小于5个字符",
                maxlength: icon + "用户账号不能大于20个字符"
            },
            password: {
                required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于5个字符",
                maxlength: icon + "密码不能大于20个字符"
            },
            confirm: {
                equalTo: icon + "请确认您的密码",
            },
            rememberme: {
                required: icon + "必须同意用户协议",
            },
        }
    })
}
